// 所有模块都通过 define 来定义
define(function(require, exports, module) {


	$('.agreementDetail').on('click', function () {
		layer.open({
			type:1,
			btn:['确认'],
			title:'酒掌柜理财注册协议',
			area: ['300px', 'auto'],
			border: [1, 1, '#ccc'],
			content: $('#modal_dialog').eq(0)
		})
	})


	$("#getEmailCodeEdit").on("click",function(){
		if(!/^1[3578]\d{9}$/.test($("#sendMobileIpt").val())){
			layer.alert("请先填写手机号码");
			return false;
		}
		var _that = this;
		$.post('../../app/user/getPhoneCode.html?mobilePhone='+$("#sendMobileIpt").val()+'&sign='+$("#sign").val(), function(data) {
			if(data.resCode==1){
				get_code_time(_that);
				$("#sign").val(data.sign);
				layer.alert("发送成功");
			}else{
				layer.alert("发送失败，请稍后再试");
			}
		});
	});
	$("#registerForm").validate({
		rules : {
			'userName' : {
				required : true,
				regexUserName:true,
				remote:{
					type: "POST",
					url: "/app/user/checkUsername.html",
					dataType: "json",
					data:{userName: function(){return $("#userName").val();}}
				}
			},
			'mobilePhone' : {
				required : true,
				isMobile:"手机号码格式错误",
				remote:{
					type: "get",
					url: "/app/user/checkMobilePhone.html",
					data:{
						mobilePhone: function(){
							return $("#mobilePhone").val();
						}
					}
				}
			},
			'pwd' : {
				required : true,
				regexPassword:true
			},
			'confirmPassword' : {
				required : true,
				minlength : 5,
				equalTo : "#pwd"
			},
			'validCode' : {
				required : true,
	   			minlength:4
			},
	   		agree:{
	   			required:true
	   		}
		},
		messages : {
			'userName' : {
				required:"用户名由英文字母、数字组成，且不能为纯数字",
				regexUserName:"用户名格式错误",
				remote:"该用户名已经存在"
			},
			'mobilePhone' : {
				required:"请输入手机号码",
				isMobile:"手机号码格式错误",
				remote:"该手机号码已存在"
			},
			'pwd' : {
				required: "8-16位字符，其中包括至少一个字母和一个数字" ,
				regexPassword:"密码格式错误"
			},
			'confirmPassword' : {
				required: "请输入确认密码",
				equalTo: "两次输入密码不一致"
			},
			'validCode' : {
				required:"请输入验证码",
	   			minlength:"验证码格式错误"
			},
			agree:{
			   	required:"请勾选服务协议条款"
			}
		},
		submitHandler : function(form, event, validator) {
			if ($("#agree:checked").length == 0) {
				layer.alert("请先阅读注册协议,并同意才能注册!");
				return;
			}
			$(form).ajaxSubmit(function(data) {
				var result = data.resCode;
				if (result == 1) {
            		$(form).slideUp();
            		$("#registerFormSec").slideDown();
            		$("#sign").val(data.sign);
            		$("#sendMobileIpt").val(data.mobilePhone);
            		$("#sendMobile").text(data.mobilePhone);
					//window.location.href = "/mobile/user/login.html";					
				} else {
					layer.alert(data.resMsg);
					$('.inputCode img').attr('src','/validimg.html?t=' + Math.random());
				}
			});
		}
	});
	
	$("#registerFormSec").validate({
		rules : {
			'code' : {
				required : true
			}
		},
		messages : {
			'code' : {
				required:"请输入验证码"
			}
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
            		$(form).slideUp();
            		$("#registerFormThird").slideDown();
            		
					//window.location.href = "/mobile/user/login.html";					
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
	});
	
	var wait = 60;
	get_code_time = function (elem) {
		if (wait == 0) {
			$(elem).text("重新发送");
			$(elem).next().hide();
			$(elem).show();
			wait = 60;
		} else {
			$(elem).next().text("("+wait+")秒后重新获取");
			$(elem).hide();
			$(elem).next().show();
			wait--;
			setTimeout(function() {
				get_code_time(elem);
			}, 1000);
		}
	};

});